﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EventMgrLib;
using Microsoft.VisualBasic.FileIO;
using SmartMotion.Core;
using Sunny.UI;
using WeifenLuo.WinFormsUI.Docking;
using UIOption = Microsoft.VisualBasic.FileIO.UIOption;

namespace SmartMotion.AllPages.DockPanels
{
    public partial class FrmSolutionMgr : DockContent
    {
        private FlowsMgr _flowsMgr;
        private EventMgr _em;

        #region 构造和Load

        public FrmSolutionMgr()
        {
            InitializeComponent();
            HideOnClose = true;
        }

        private void FrmSolutionMgr_Load(object sender, EventArgs e)    
        {
            _flowsMgr=FlowsMgr.Ins;
            _em=EventMgr.Ins;
            _em.GetEvent<FlowEvent>().Subscribe(FileOperate);
            ShowFlows();
        }

        #endregion

        private void ShowFlows()
        {
            tv.Nodes.Clear();
            var groups = _flowsMgr.Flows.Select(flow => flow.Group).ToList().Distinct().ToList();
            foreach (var gpName in groups)
            {
                var gpNode = new TreeNode()
                {
                    Text = string.IsNullOrEmpty(gpName) ? "Default" : gpName,
                    ImageIndex = 0,
                    SelectedImageIndex = 0,
                };

                foreach (var flow in _flowsMgr.Flows)
                {
                    if (flow.Group!=gpName) continue;
                    var flowNode = new TreeNode(flow.Name)
                    {
                        ImageIndex = 1,
                        SelectedImageIndex = 1,
                    };
                    gpNode.Nodes.Add(flowNode);
                }

                tv.Nodes.Add(gpNode);
            }
            tv.ExpandAll();
        }

        private void CreateFlowItem_Click(object sender, EventArgs e)
        {
            string groupName = string.Empty;
            if (!UIInputDialog.InputStringDialog(ref groupName,true,"请输入分组名称")) return;
            string newFlowName = string.Empty;
            if (UIInputDialog.InputStringDialog(ref newFlowName, true,"请输入流程名称"))
            {
                if (_flowsMgr.Flows.Any(flow => flow.Name == newFlowName))
                {
                    UIMessageBox.ShowError($"{newFlowName}已存在"); return;
                }

                var newFlow = new Flow()
                {
                    Name = newFlowName,
                    Group = groupName
                };
                _flowsMgr.Flows.Add(newFlow);
                ShowFlows();
                _em.GetEvent<ShowEditFrm>().Publish(newFlow);
            }
        }

        private void tv_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var findFlow = _flowsMgr.Flows.Find(flow => flow.Name == e.Node.Text);
            if (findFlow!=null)
            {
                _em.GetEvent<ShowEditFrm>().Publish(findFlow);
            }
        }

        private void FileOperate(FileOpt fileOpt)
        {
            switch (fileOpt)
            {
                case FileOpt.None:
                    break;
                case FileOpt.Create:
                    break;
                case FileOpt.Open:
                    break;
                case FileOpt.Save:
                    _flowsMgr.SaveFlows();
                    break;
                case FileOpt.SaveAs:
                    break;
                case FileOpt.Add:
                    break;
                case FileOpt.Del:
                    break;
                case FileOpt.Edit:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(fileOpt), fileOpt, null);
            }    
        }

        private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var findFlow = _flowsMgr.Flows.Find(flow => flow.Name == e.Node.Text);
            if (findFlow != null)
            {
                _em.GetEvent<SelectFlowChanged>().Publish(findFlow);
            }
        }

        private void 删除流程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var node = tv.SelectedNode;
            if (node.Nodes.Count>0) return;
            var item = _flowsMgr.Flows.Find(flow => flow.Name == tv.SelectedNode.Text);
            if (item==null) return;
            if (UIMessageBox.ShowAsk($"是否将流程{item.Name}放入回收站?"))
            {
                string filePath = $"{_flowsMgr.FlowsPath}{item.Name}.flow";
                if (File.Exists(filePath))
                {
                    FileSystem.DeleteFile(filePath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
                    _flowsMgr.Flows.Remove(item);
                    _flowsMgr.SaveFlows();
                    ShowFlows();
                }
            }
        }
    }
}
    